package xginxgodb

import (
	"encoding/json"

	"gitee.com/xiaoyutab/xgotool/xgin"
	"gitee.com/xiaoyutab/xgotool/xgodb"
	"gitee.com/xiaoyutab/xgotool/xstring"
	"github.com/gin-gonic/gin"
)

// 设置配置项信息
//
//	POST	set.configure
func setConfigure(c *gin.Context) *xgin.Response {
	type temp_struct struct {
		NameKey     string      `json:"name_key" form:"name_key"`         //配置项key
		Types       string      `json:"types" form:"types"`               //配置项类型
		ValueBool   json.Number `json:"value_bool" form:"value_bool"`     // bool类型的值存储 uint8
		ValueString string      `json:"value_string" form:"value_string"` // 字符串类型配置
		ValueInt    json.Number `json:"value_int" form:"value_int"`       // int类型配置
		ValueFloat  json.Number `json:"value_float" form:"value_float"`   // float类型配置
		EnumSelect  string      `json:"enum_select" form:"enum_select"`   //下拉列表，多项使用,分割key和key_cn使用:分割，下拉选择的值存储在value_string中
	}
	temp := temp_struct{}
	if err := xgin.Bind(c, &temp); err != nil {
		return xgin.Resp().Error(err)
	}
	var err error
	switch temp.Types {
	case "bool":
		err = xgodb.SetBool(temp.NameKey, xstring.JUint8(temp.ValueBool) == 1)
	case "string":
		err = xgodb.SetString(temp.NameKey, temp.ValueString)
	case "select":
		err = xgodb.SetSelect(temp.NameKey, temp.ValueString)
	case "text":
		err = xgodb.SetText(temp.NameKey, temp.ValueString)
	case "longtext":
		err = xgodb.SetLongText(temp.NameKey, temp.ValueString)
	case "int":
		err = xgodb.SetInt(temp.NameKey, xstring.JInt64(temp.ValueInt))
	case "float":
		err = xgodb.SetFloat(temp.NameKey, xstring.JFloat64(temp.ValueFloat))
	}
	if err != nil {
		return xgin.Resp().Error(err)
	}
	return xgin.Resp().SuccessEmpty()
}
